package 二叉树的最近公共祖先;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 二叉搜索树的最近公共祖先
 * 根据二叉搜索树的特性遍历
 * @date 2022/11/7 10:09
 */
public class Demo235 {
    //1.递归函数就是主函数
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
       //2.确定返回值
       if(root==null){
           return null;
       }
       //3.开始遍历
        if(root.val> p.val&&root.val> q.val){
            root=lowestCommonAncestor(root.left,p,q);
        }else if(root.val< p.val&&root.val<q.val){
            root=lowestCommonAncestor(root.right,p,q);
        }
        return root;
    }


    //Definition for a binary tree node.
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

}
